package com.lyk.coursearrange.util;

import com.lyk.coursearrange.entity.OnlineCategory;
import io.swagger.models.auth.In;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class TreeUtils<T> {

    public static <T> List<T> getTree(T list) throws IllegalAccessException {
        List<Integer> parentIds = new ArrayList<>();


        for (Field declaredField : list.getClass().getDeclaredFields()) {
            declaredField.setAccessible(true);
            System.err.println(declaredField.getName());
            System.err.println(declaredField.get("parentId"));

            parentIds.add((Integer) declaredField.get("parentId"));
        }


//        for (T result : list.getClass()) {
//            if (result.getParentId() == 0) {
//                //调用方法给子类添加数据
//                resultList.add(getMenuTree(result, list));
//            }
//        }
        return null;
    }

    /**
     * 递归查询子节点
     * @return 根节点信息
     */
    private OnlineCategory getMenuTree(OnlineCategory result, List<OnlineCategory> list) {
        for (OnlineCategory sysDepartManage : list) {
            //如果父类主键等于传过来实体类的ID
            if (sysDepartManage.getParentId().equals(result.getId())) {
                if (result.getChildren() == null) {
                    result.setChildren(new ArrayList<>());
                }
                // 递归调用
                result.getChildren().add(getMenuTree(sysDepartManage, list));
            }
        }
        return result;
    }
}
